package com.ollobot.authorization.common;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.util.HashMap;
import java.util.Map;

public class TimestampResponseEntity<T> extends ResponseEntity<Map<String, Object>> {
    
    public TimestampResponseEntity(T data, HttpStatus status) {
        super(createBodyWithTimestamp(data, null, null, status), status);
    }
    
    public TimestampResponseEntity(T data, HttpStatus status, String message) {
        super(createBodyWithTimestamp(data, message, null, status), status);
    }
    
    public TimestampResponseEntity(T data, HttpStatus status, String message, Integer code) {
        super(createBodyWithTimestamp(data, message, code, status), status);
    }
    
    public TimestampResponseEntity(T data, HttpHeaders headers, HttpStatus status) {
        super(createBodyWithTimestamp(data, null, null, status), headers, status);
    }
    
    public TimestampResponseEntity(T data, HttpHeaders headers, HttpStatus status, String message, Integer code) {
        super(createBodyWithTimestamp(data, message, code, status), headers, status);
    }
    
    public TimestampResponseEntity(T data) {
        this(data, HttpStatus.OK);
    }
    
    @SuppressWarnings("unchecked")
    private static <T> Map<String, Object> createBodyWithTimestamp(T data, String message, Integer code, HttpStatus status) {
        Map<String, Object> responseBody = new HashMap<>();
        
        // 设置状态信息
        responseBody.put("success", status.is2xxSuccessful());
        
        // 设置业务编码或HTTP状态码
        if (code != null) {
            responseBody.put("code", code);
        } else {
            responseBody.put("code", status.value());
        }
        
        // 设置消息或错误信息
        if (message != null) {
            if (status.is2xxSuccessful()) {
                responseBody.put("message", message);
            } else {
                responseBody.put("error", message);
            }
        }
        
        // 设置业务数据
        if (data != null) {
            responseBody.put("data", data);
        }
        
        // 添加时间戳
        responseBody.put("timestamp", System.currentTimeMillis());
        
        return responseBody;
    }
    
    // 静态工厂方法 - 成功响应
    public static <T> TimestampResponseEntity<Map<String, Object>> success(T data) {
        Map<String, Object> responseBody = new HashMap<>();
        responseBody.put("success", true);
        responseBody.put("code", 200);
        if (data != null) {
            responseBody.put("data", data);
        }
        return new TimestampResponseEntity<>(responseBody, HttpStatus.OK);
    }
    
    public static <T> TimestampResponseEntity<Map<String, Object>> success(T data, String message) {
        Map<String, Object> responseBody = new HashMap<>();
        responseBody.put("success", true);
        responseBody.put("code", 200);
        responseBody.put("message", message);
        if (data != null) {
            responseBody.put("data", data);
        }
        return new TimestampResponseEntity<>(responseBody, HttpStatus.OK);
    }
    
    public static TimestampResponseEntity<Map<String, Object>> success(String message) {
        Map<String, Object> responseBody = new HashMap<>();
        responseBody.put("success", true);
        responseBody.put("code", 200);
        responseBody.put("message", message);
        return new TimestampResponseEntity<>(responseBody, HttpStatus.OK);
    }
    
    // 静态工厂方法 - 错误响应
    public static <T> TimestampResponseEntity<T> error(int code, String message, T data) {
        return new TimestampResponseEntity<>(data, HttpStatus.BAD_REQUEST, message, code);
    }
    
    public static <T> TimestampResponseEntity<T> error(int code, String message) {
        return new TimestampResponseEntity<>(null, HttpStatus.BAD_REQUEST, message, code);
    }
    
    public static <T> TimestampResponseEntity<T> error(int code, String message, HttpStatus status) {
        return new TimestampResponseEntity<>(null, status, message, code);
    }
    
    // 便捷方法 - 使用默认400错误码
    public static <T> TimestampResponseEntity<T> error(String message) {
        return error(400, message);
    }
    
    public static <T> TimestampResponseEntity<T> error(String message, HttpStatus status) {
        return error(status.value(), message, status);
    }
    
    // 自定义状态码响应
    public static <T> TimestampResponseEntity<Map<String, Object>> withStatus(T data, HttpStatus status) {
        Map<String, Object> responseBody = new HashMap<>();
        responseBody.put("success", status.is2xxSuccessful());
        responseBody.put("code", status.value());
        if (data != null) {
            responseBody.put("data", data);
        }
        return new TimestampResponseEntity<>(responseBody, status);
    }
}
